package com.xm.xmapp;

import java.io.File;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.dialog.TipLeftRightDialog;
import com.xm.xmapp.http.UpdataAppClass;
import com.xm.xmapp.http.UpdataAppClass.HandlerInterFace;
import com.xm.xmapp.myinterface.DialogInterFace;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.VersionRequest;
import com.xm.xmapp.response.bean.JpushNotifactionResponse;
import com.xm.xmapp.response.bean.ResponseVersion;
import com.xm.xmapp.response.bean.ResponseVersion.T;
import com.xm.xmapp.tools.Helper_SharedPreferences;
import com.xm.xmapp.tools.Tools;

public class StartActivity extends BaseActivity implements OnClickListener {

	private LinearLayout lin;
	private TipLeftRightDialog mydialog;
	private ResponseVersion bean;// 版本检测更新
	private T t;
	private JpushNotifactionResponse mJpushNotifactionResponse;// 极光推送，后台打开app，主页弹出dialog

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		/**
		 * 网页打开app，如果app在后台运行，则移除所有页面,重新启动
		 */
//		MyAppcation.getMyAppcation().reMoveAllBaseInterFace();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start);

	}

	@Override
	public void initView() {
		lin = (LinearLayout) findViewById(R.id.lin);
		getData();
	}

	private void getData() {

		mJpushNotifactionResponse = (JpushNotifactionResponse) getIntent().getSerializableExtra("bean");
		VersionRequest requestBean = new VersionRequest();
//		VersionRequest requestBean  = null;
		requestBean.app_version_number = Tools.getVersionCode();
		requestBean.phone_model = Tools.getMODEL();

		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				bean = JSON.parseObject(response, ResponseVersion.class);
				UpdataAppClass.getUpdataAppClass().setQuxiao(false);
				if (bean.is_version_update == 1) {
					t = JSON.parseObject(bean.version_info, ResponseVersion.T.class);
					UpdataAppClass.APK_NAME = "yibuyongche_" + t.app_show_version_number;
					File file = new File(MyAppcation.getMyAppcation().getExternalFilesDir("myapk") + "/",
							UpdataAppClass.APK_NAME + ".apk");
					if (!file.exists()) {
						if (t.is_force_update == 1) {
							showBuilder("有新版本", "文件大小:" + t.app_file_size + "\n" + t.app_version_description, 1);
						} else {
							showBuilder("有新版本", "文件大小:" + t.app_file_size + "\n" + t.app_version_description, 1);
						}
					} else {
						UpdataAppClass.getUpdataAppClass().update();
						lin.setOnClickListener(StartActivity.this);
					}

				} else {
					begin();
				}
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				begin();
			}
		});
	}

	protected void showBuilder(String title, String message, int flag) {

		mydialog = new TipLeftRightDialog(this, title, message, flag);
		mydialog.setCancelable(false);
		mydialog.setDialogInterFace("取消", "下载", new DialogInterFace() {

			@Override
			public void right(int flag, Object data) {

				UpdataAppClass.getUpdataAppClass().downFile(t.url, StartActivity.this, new HandlerInterFace() {

					@Override
					public void run(Message msg) {
						lin.setOnClickListener(StartActivity.this);
						finish();
					}
				});
				mydialog.dismiss();
			}

			@Override
			public void left(int flag, Object data) {

				if (t != null && t.is_force_update == 1) {
					finish();
				} else {
					begin();
				}
				mydialog.dismiss();
			}
		}).show();

	}

	private void begin() {
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				startToNext();
			}
		}, CodeConstant.time);
	}

	/**
	 * 跳转
	 */
	protected void startToNext() {

		if (Helper_SharedPreferences.get_bool_sp(CodeConstant.YseOrNo)) {
			intent = new Intent(MyAppcation.getMyAppcation(), MainActivity.class);
			intent.putExtra("bean", mJpushNotifactionResponse);
			startActivity(intent);
			overridePendingTransition(R.anim.welcome_activity_in, R.anim.welcome_activity_out);
		} else {
			intent = new Intent(MyAppcation.getMyAppcation(), GuideActivity.class);
			startActivity(intent);
		}
		finish();
	}

	@Override
	public void initData() {

	}

	@Override
	public void initEvent() {

	}

	@Override
	public void onClick(View arg0) {

		switch (arg0.getId()) {
		case R.id.lin:
			startToNext();
			break;

		default:
			break;
		}
	}

}
